package com.briup.day41;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * 客户端*/
public class ClientDemo1 {
	
	public static void main(String[] args) {
		
		String ip = "127.0.0.1";
		int port = 8888;
		try {
	
			Socket socket = new Socket(ip, port);
			System.out.println("连接服务器成功!");
			
			//向服务器发信息
			OutputStream os = socket.getOutputStream();
			os.write("hello,server!".getBytes());
			//os.close();
			socket.shutdownOutput();
			
			//接收服务器发回的消息
			InputStream is = socket.getInputStream();
			int i = -1;
			System.out.println("【服务器端:】");
			while ((i = is.read()) != -1) {
				System.out.print((char)i);
			}
			
		} catch (UnknownHostException e) {
			 
			e.printStackTrace();
		} catch (IOException e) {
			 
			e.printStackTrace();
		}
	}

}
